home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX501 / WINCAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  4.8 KB  |  337 lines

  1. /* wincap.c */
  2.  
  3. /*
  4.  * Winchester Disk `capability' file parser.
  5.  *
  6.  *    int wgetent(name);
  7.  *    char *name;
  8.  *
  9.  *    int wallents(buf);
  10.  *    char *buf;
  11.  *
  12.  *    int wgetnum(id, anum);
  13.  *    char *id;
  14.  *    int *anum;
  15.  *
  16.  *    int wgetflag(id);
  17.  *    char *id;
  18.  *
  19.  *    char *wgetstr(id);
  20.  *    char *id;
  21.  *
  22.  */
  23. #include "obdefs.h"
  24. #include "gemdefs.h"
  25. #include "osbind.h"
  26. #include "mydefs.h"
  27. #include "hdx.h"
  28. #include "addr.h"
  29.  
  30.  
  31. #define    WBUFSIZE    1024
  32.  
  33. extern char toupper();
  34. extern char *getln();
  35. extern cgetln();
  36.  
  37.  
  38. char *wgetstr();
  39. char *wfindid();
  40.  
  41. static char wbuf[WBUFSIZE];
  42. static int wcapfd;
  43. static int wcapopen = 0;
  44.  
  45.  
  46. /*
  47.  * Get an entry into the buffer.
  48.  *
  49.  */
  50. wgetent(name, id)
  51. char *name;
  52. char *id;
  53. {
  54.     char *nm;
  55.  
  56.     if (wcapok() < 0) return ERROR;
  57.     wreset();
  58.  
  59.     if (id == NULL)
  60.     id = "mn";
  61.  
  62.     while (nextentry() == OK)
  63.     {
  64.     nm = wgetstr(id);
  65.     if (nm != NULL &&
  66.         !strcmp(nm, name)) return OK;
  67.     }
  68.     return ERROR;
  69. }
  70.  
  71.  
  72. /*
  73.  * Seek to beginning of wincap file
  74.  * and reset the line demon.
  75.  *
  76.  */
  77. wreset()
  78. {
  79.     Fseek(0L, wcapfd, 0);
  80.     getln(-1);
  81. }
  82.  
  83.  
  84. /*
  85.  * Get a list of all wcap entries;
  86.  * stuff into the buffer, seperated by nulls;
  87.  * final null marks last entry.
  88.  *
  89.  */
  90. wallents(buf, id)
  91. char *buf;
  92. char *id;
  93. {
  94.     char *nm;
  95.  
  96.     if (id == NULL)
  97.     id = "mn";
  98.  
  99.     if (wcapok() < 0) return ERROR;
  100.     wreset();
  101.  
  102.     while (nextentry() == OK)
  103.     {
  104.     if ((nm = wgetstr(id)) == NULL)
  105.         continue;
  106.     while (*nm)
  107.         *buf++ = *nm++;
  108.     *buf++ = '\0';
  109.     }
  110.  
  111.     *buf++ = '\0';
  112.     *buf = '\0';
  113.     return OK;
  114. }
  115.  
  116.  
  117. /*
  118.  * Get number from wcap entry.
  119.  *
  120.  */
  121. int wgetnum(id, anum)
  122. char *id;
  123. long *anum;
  124. {
  125.     char *ent;
  126.  
  127.     if ((ent = wfindid(id)) == NULL ||
  128.     *ent++ != '#')
  129.         return ERROR;
  130.     return getnum(ent, anum);
  131. }
  132.  
  133.  
  134. /*
  135.  * Get flag from wcap entry.
  136.  *
  137.  */
  138. wgetflag(id)
  139. char *id;
  140. {
  141.     char *ent;
  142.  
  143.     if ((ent = wfindid(id)) == NULL)
  144.         return ERROR;
  145.  
  146.     return OK;
  147. }
  148.  
  149.  
  150. /*
  151.  * Get string from wcap entry;
  152.  * return NULL if the id doesn't exist.
  153.  *
  154.  */
  155. char *wgetstr(id)
  156. char *id;
  157. {
  158.     char *ent;
  159.  
  160.     if ((ent = wfindid(id)) == NULL ||
  161.     *ent++ != '=')
  162.         return NULL;
  163.  
  164.     return ent;
  165. }
  166.  
  167.  
  168. /*
  169.  * Find an id;
  170.  * return pointer to rest of item (or NULL).
  171.  *
  172.  */
  173. char *wfindid(id)
  174. char *id;
  175. {
  176.     char *s;
  177.     char *skipstr();
  178.     int i=0, j;
  179.  
  180.     s = &wbuf[0];
  181.     s = skipstr(s);
  182.     while (*s)
  183.     {
  184.         for (j = 0; ; j++, i++)    {
  185.             if ((!id[j]) && ((s[i] == '=')||(s[i] == '#')))
  186.                 return(s+i);
  187.             if (s[i] == id[j])
  188.                 continue;
  189.             else
  190.                 break;
  191.         }
  192.         s = skipstr(s);
  193.     }
  194.     return NULL;
  195. }
  196.  
  197.  
  198. /*
  199.  * Skip string;
  200.  * return ptr to thing past the string's `\0'.
  201.  *
  202.  */
  203. char *skipstr(s)
  204. char *s;
  205. {
  206.     while (*s) ++s;
  207.     return ++s;
  208. }
  209.  
  210.  
  211. /*
  212.  * Get next entry from wincap file;
  213.  * return ERROR if no more entries.
  214.  *
  215.  * Seperating `:'s are turned into \0,
  216.  * `\' can prevent that.
  217.  *
  218.  ***************** ***************** ***************** *****************
  219.  * Awww, shucks -- we need to do a "string delete" for that.  So '\'
  220.  * doesn't work yet.
  221.  ***************** ***************** ***************** *****************
  222.  *
  223.  */
  224. nextentry()
  225. {
  226.     char *s;
  227.  
  228.     while (cgetln(wcapfd, wbuf))
  229.     {
  230.     if (wbuf[0] == '#' ||        /* comment or empty line */
  231.         wbuf[0] == '\0')
  232.         continue;
  233.  
  234.     for (s = wbuf; *s; ++s)
  235.         switch (*s)
  236.         {
  237.         case '\\':    ++s;
  238.                 break;
  239.  
  240.         case ':':    *s = '\0';
  241.                 break;
  242.  
  243.         default:    continue;
  244.         }
  245.     *s++ = '\0';
  246.     return OK;
  247.     }
  248.  
  249.     return ERROR;
  250. }
  251.  
  252.  
  253. /*
  254.  * If the wcap file isn't open, then open it;
  255.  * return ERROR if the file won't open.
  256.  *
  257.  */
  258. wcapok()
  259. {
  260.     extern int running;
  261.  
  262.     if (!wcapopen &&
  263.     (wcapfd = (int)Fopen(WCAPFILE, 0)) < 0)
  264.     {
  265.     running = 0;
  266.     return err(nowincap);
  267.     }
  268.  
  269.     wcapopen = 1;
  270.     return OK;
  271. }
  272.  
  273.  
  274. /*
  275.  * Parse number;
  276.  *    octal numbers start with a leading `0';
  277.  *    decimal numbers start with digits;
  278.  *    hex numbers start with `0x' or `$';
  279.  *    numbers may be terminated with a `k' (1,000)
  280.  *    or `m' (1,000,000) multiplier.
  281.  *
  282.  */
  283. getnum(s, av)
  284. char *s;
  285. long *av;
  286. {
  287.     static char numtab[] = "0123456789abcdefABCDEF";
  288.     int base, i;
  289.     long v;
  290.  
  291.     base = 10;
  292.     v = 0L;
  293.     if(*s == '$')
  294.     {
  295.     ++s;
  296.     base = 16;
  297.     } else if (*s == '0' && toupper(s[1]) == 'X')
  298.     {
  299.     s += 2;
  300.     base = 16;
  301.     } else if (*s == '0')
  302.     base = 8;
  303.  
  304.     while(*s)
  305.     {
  306.     for(i = 0; numtab[i]; ++i)
  307.         if(*s == numtab[i]) break;
  308.     if(!numtab[i]) break;
  309.     if(base == 16 && i >= 16) i-=6;
  310.     if(i >= base) return ERROR;
  311.     v = (v * base) + i;
  312.     ++s;
  313.     }
  314.  
  315.  
  316.     /* handle multiplier */
  317.     switch (toupper(*s))
  318.     {
  319.     case 'K':
  320.         v *= 1024L;
  321.         break;
  322.  
  323.     case 'M':
  324.         v *= (1024L * 1024L);
  325.         break;
  326.  
  327.     case '\0':
  328.         break;
  329.  
  330.     default:
  331.         return ERROR;
  332.     }
  333.  
  334.     *av = v;
  335.     return OK;
  336. }
  337.